###############################################
#                                             #
#     National Survey Treatment Effects       #
# Using Only the 3000 Representative Sample   #
#     Created by Mac Lockhart Nov 30 2022     #
#     Updated Nov 30 2022                     #
#                                             #
###############################################

library(tidyverse)
library(modelsummary)

#Read in most recent version of cleaned data using:
    #Clean_National_Sample.R
if(Sys.info()[7] == "maclockhart"){
  setwd("/Users/maclockhart/Dropbox/School/RA Work/'22 Seth & Thad/MIT Evolving Election Administration Landscape")
}
if(Sys.info()[7] == "ml2999"){
  setwd("E:/Dropbox/School/RA Work/'22 Seth & Thad/MIT Evolving Election Administration Landscape")
}
DF<- read_csv("Survey Data/National Recoded.csv")

#Recode outcomes as change from pre-treatment values following the pre-analysis plan
DF$ownstate <- DF$ownstatepost-DF$ownstatepre
DF$otherstates <- DF$otherstatepost - DF$otherstatepre
DF$votefraud <- DF$votefraudpost - DF$votefraudpre
DF$officialfraud <- DF$officialfraudpost - DF$officialfraudpre
DF$vote2024 <- DF$vote2024post - DF$vote2024pre

#Pooled treatment effects by video
m1.1 <- lm(ownstate ~ d_treatment, DF)
m1.2 <- lm(otherstates ~ d_treatment, DF)
m1.3 <- lm(votefraud ~ d_treatment, DF)
m1.4 <- lm(officialfraud ~ d_treatment, DF)
m1.5 <- lm(vote2024 ~ d_treatment, DF)

#Separate treatment effects by video
#m2.1 <- lm(ownstate ~ treatment, DF)
#m2.2 <- lm(otherstates ~ treatment, DF)
#m2.3 <- lm(votefraud ~ treatment, DF)
#m2.4 <- lm(officialfraud ~ treatment, DF)
#m2.5 <- lm(vote2024 ~ treatment, DF)

binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Vote Fraud Belief" = m1.3,
                      "Officials Fraud Belief" = m1.4,
                      "2024 Vote Intent" = m1.5)
modelsummary(binary_models, output = "Mac and Jen Results/Treatment Effects/national_only_binary.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             #coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')



######Treatment interactions############

DF <- fread("Survey Data/National Recoded.csv")
CO <- fread("Survey Data/Colorado Recoded.csv")
GA <- fread("Survey Data/Georgia Recoded.csv")
LA <- fread("Survey Data/LA Recoded.csv")
TX <- fread("Survey Data/Texas Recoded.csv")

DF$bot <- 0
DF$n<-1:nrow(DF)
#assign sample names
DF$sample<-"USA"
CO$sample<-"CO"
GA$sample<-"GA"
LA$sample <- "LA"
TX$sample <-"TX"

#Remove bots
DF<-rbindlist(list(DF, CO, GA, LA, TX), fill=T)
rm(CO, GA, LA, TX)
DF<-DF[bot==0]

#Recode outcomes as change from pre-treatment values following the pre-analysis plan
DF$ownstate <- DF$ownstatepost-DF$ownstatepre
DF$otherstates <- DF$otherstatepost - DF$otherstatepre
DF$votefraud <- DF$votefraudpost - DF$votefraudpre
DF$officialfraud <- DF$officialfraudpost - DF$officialfraudpre
DF$vote2024 <- DF$vote2024post - DF$vote2024pre
DF$anystate_treatment <- ifelse(DF$state_treatment=="Control", 0, 1)


#Pooled treatment effects by video
m1.1 <- lm(ownstate ~ d_treatment*anystate_treatment, DF)
m1.2 <- lm(otherstates ~ d_treatment*anystate_treatment, DF)
m1.3 <- lm(votefraud ~ d_treatment*anystate_treatment, DF)
m1.4 <- lm(officialfraud ~ d_treatment*anystate_treatment, DF)
m1.5 <- lm(vote2024 ~ d_treatment*anystate_treatment, DF)

#Separate treatment effects by video
#m2.1 <- lm(ownstate ~ treatment, DF)
#m2.2 <- lm(otherstates ~ treatment, DF)
#m2.3 <- lm(votefraud ~ treatment, DF)
#m2.4 <- lm(officialfraud ~ treatment, DF)
#m2.5 <- lm(vote2024 ~ treatment, DF)

binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Vote Fraud Belief" = m1.3,
                      "Officials Fraud Belief" = m1.4,
                      "2024 Vote Intent" = m1.5)
modelsummary(binary_models, output = "Mac and Jen Results/Treatment Effects/full_interacted.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             #coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')